package fran.pinggoogle;


import fran.pinggoogle.network.PingMaker;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PingGooglePrincipal extends ListActivity {

	private PingAdapter adapter;
	private Button ping;
	private PingMaker pingMaker;
	private PingGooglePrincipal mThis;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_ping_google);
		mThis = this;
		adapter = new PingAdapter(this);

		setListAdapter(adapter);
		
		ping = (Button)findViewById(R.id.btnNuevoPedido);
		
		ping.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(ping.getText().toString().compareTo(getResources().getText(R.string.labelComenzarPing).toString()) == 0){
					pingMaker = new PingMaker();
					pingMaker.pingGoogle(mThis);
					ping.setText(getResources().getText(R.string.labelDetenerPing).toString());
				}else if(ping.getText().toString().compareTo(getResources().getText(R.string.labelDetenerPing).toString()) == 0){
					pingMaker.setBandera(false);
					ping.setText(getResources().getText(R.string.labelComenzarPing).toString());
				}
			}
		});
	}
	
//	@Override
//    protected void onPause() {
//        super.onPause();
//        pingMaker.setBandera(false);
//		ping.setText(getResources().getText(R.string.labelComenzarPing).toString());
//    }

	
	public void addPingResult(StringBuffer pingResult){
		adapter.addPingResult(pingResult.toString());
		adapter.notifyDataSetChanged();
		getListView().post(new Runnable() {
	        @Override
	        public void run() {
	            // Select the last row so it will scroll into view...
	            getListView().setSelection(adapter.getCount() - 1);
	        }
	    });
	}
}
